package com.ihelp.controller;

import com.ihelp.annotation.UserCheckLogin;
import com.ihelp.annotation.UserCheckOperate;
import com.ihelp.entities.CommonResult;
import com.ihelp.pojo.*;
import com.ihelp.service.OrderService;
import com.ihelp.service.RequestService;
import com.ihelp.service.UserService;
import com.ihelp.util.CookieUtil;
import com.ihelp.util.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-05-23
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RequestService requestService;
    @Autowired
    private UserService userService;


    @GetMapping("/all")
    public CommonResult getAllOrder() {
        List<OrderDTO> allOrder = orderService.getAllOrder();
        return new CommonResult(200, "获取所有订单成功", allOrder);
    }

    @UserCheckOperate(operateObject = "order")
    @GetMapping("/id/{id}")
    public CommonResult getOrderById(@PathVariable Integer id) {
        OrderDTO orderById = orderService.getOrderById(id);
        if (orderById != null) {
            return new CommonResult<>(200, "根据id:" + id + ",获取订单成功", orderById);
        } else {
            return new CommonResult<>(204, "根据id:" + id + ",未找到订单");
        }
    }

    @UserCheckLogin
    @PostMapping("/")
    public CommonResult createOrder(@RequestParam("requestId") Integer requestId,
                                     HttpServletRequest req) {
        try {
            String userJWT = CookieUtil.getCookies(req, "userJWT");
            Claims claims = jwtUtil.parseJWT(userJWT);
            Integer userId = claims.get("id", Integer.class);
            User userById = userService.getUserById(userId);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date currentDate = new Date();
            String orderTime = sdf.format(currentDate);

            RequestDTO requestById = requestService.getRequestById(requestId);
            Integer visitorId = requestById.getVisitorId();
            Integer serviceTypeId = requestById.getServiceTypeId();
            Double duration = requestById.getDuration();
            Integer hospitalId = requestById.getHospitalId();
            Integer hospitalDepartmentId = requestById.getHospitalDepartmentId();
            String visitTime = requestById.getVisitTime();

            String visitInformation = "";


            int orderResult = orderService.createOrder(visitorId, userId, orderTime, visitTime, duration, serviceTypeId, visitInformation, hospitalId, hospitalDepartmentId);
            if (orderResult == 1) {
                return new CommonResult<>(200, "创建订单成功");
            } else {
                return new CommonResult<>(400, "创建订单失败");
            }
        } catch (Exception e) {
            return new CommonResult(401, "用户验证信息错误");
        }





    }

    @UserCheckOperate(operateObject = "order")
    @GetMapping("/user/{id}")
    public CommonResult getOrderByUserId(@PathVariable Integer id){
        List<OrderDTO> orderAsVisitor = orderService.getOrderByDynamicConditions(null, id, null, null, null,
                null, null, null, null, null, null,
                null,null);
        List<OrderDTO> orderAsAccompanyer = orderService.getOrderByDynamicConditions(null, null, id, null, null,
                null, null, null, null, null, null,
                null,null);
        orderAsVisitor.addAll(orderAsAccompanyer);
        List<OrderDTO> orders = orderAsVisitor;
        if(orders.isEmpty()){
            return new CommonResult<>(204, "根据用户id:" + id + ",未找到对应订单");
        }else {
            return new CommonResult<>(200, "根据用户id:" + id + ",获取所有订单成功", orders);
        }
    }
}
